package logic

import (
	"context"
	"github.com/google/uuid"
	"shop_srv/models"

	"shop_srv/internal/svc"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateOrderLogic) CreateOrder(in *shop.CreateOrderRequest) (*shop.CreateOrderResponse, error) {
	var o models.Order
	o.OrderSn = uuid.NewString()
	o.UserId = int(in.UserId)
	o.GoodsId = int(in.GoodsId)
	o.OrderPrice = float64(in.OrderPrice)
	err := o.Create()
	if err != nil {
		logx.Error("订单生成失败", err)
		return nil, err
	}
	return &shop.CreateOrderResponse{
		OrderId: int64(o.ID),
	}, nil
}
